/*
 * Created by JFormDesigner on Tue Jun 25 12:46:07 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.UserDao;
import scct.hys.model.User;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.*;

/**
 * @author 26984
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
        initView();
    }

    private void initView() {
        UserDao userDao=new UserDao();
        List<User> allUser=userDao.getAllUser();
        for (User user:allUser){
            txtName.addItem(user.getName());

        }
    }


    private void btnLogin(ActionEvent e) throws SQLException {
        // TODO add your code here
        String name= (String) txtName.getSelectedItem();
        String pass=txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式");
//    返回true表示成功，返回false表示验证失败
        if (!name.matches("^\\w{3,10}$")){
//            swing的消息对话框
            JOptionPane.showConfirmDialog(this,"你输入的用户名不合法，请重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(this,"你输入的密码不合法，请重新输入");
            return;
        }
        UserDao userDao=new UserDao();
        User user=userDao.Login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后重新登录");
        }else {
            JOptionPane.showMessageDialog(this,"登录成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
            this.setLocationRelativeTo(null);
            formMain.setVisible(true);
            this.dispose();
            System.out.println(btnLogin);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        label4 = new JLabel();
        txtName1 = new JLabel();
        label6 = new JLabel();
        txtPass = new JTextField();
        btnLogin = new JButton();
        btnReg = new JButton();
        txtName = new JComboBox();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label4 ----
        label4.setText("\u767b\u5f55");
        label4.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 29));
        contentPane.add(label4);
        label4.setBounds(160, 20, 75, 35);

        //---- txtName1 ----
        txtName1.setText("\u7528\u6237\u540d");
        txtName1.setFont(txtName1.getFont().deriveFont(txtName1.getFont().getSize() + 3f));
        contentPane.add(txtName1);
        txtName1.setBounds(new Rectangle(new Point(55, 85), txtName1.getPreferredSize()));

        //---- label6 ----
        label6.setText("\u5bc6\u7801");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 3f));
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(60, 135), label6.getPreferredSize()));
        contentPane.add(txtPass);
        txtPass.setBounds(125, 130, 185, 30);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> {try {
btnLogin(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(85, 185), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(245, 185), btnReg.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(125, 85, 185, txtName.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JLabel label4;
    private JLabel txtName1;
    private JLabel label6;
    private JTextField txtPass;
    private JButton btnLogin;
    private JButton btnReg;
    private JComboBox txtName;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
